
package com.yimixian.app.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.yimixian.app.R;
import com.yimixian.app.ui.wheelview.OnWheelChangedListener;
import com.yimixian.app.ui.wheelview.WheelView;
import com.yimixian.app.ui.wheelview.adapter.ArrayWheelAdapter;
import com.yimixian.app.util.Strings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 轮子弹出框
 * 
 * @author zhangchao
 */
public class WheelViewDialogNew extends Dialog implements View.OnClickListener,
        OnWheelChangedListener {

    private LinearLayout ll_wheel;
    private List<Map<String, Object>> dataListMap = new ArrayList<>();
    private Handler handler = new Handler();
    private List<Map<String, Object>> selectedDataList = new ArrayList<>();
    private Map<String, List<Map<String, Object>>> dataMap = new HashMap<>();
    private String[] chooseDatas;

    public WheelViewDialogNew(Context context, List<Map<String, Object>> dataListMap) {
        super(context, R.style.DialogStyle);
        this.dataListMap = dataListMap;
        initView(context);
    }

    public WheelViewDialogNew(Context context, String[] chooseDatas, List<Map<String, Object>> dataListMap) {
        super(context, R.style.DialogStyle);
        this.dataListMap = dataListMap;
        this.chooseDatas = chooseDatas;
        initView(context);
    }

    private void initNextLevelData(List<Map<String, Object>> dataListMap){
        initNextLevelData(dataListMap, "-1");
    }

    private void initNextLevelData(List<Map<String, Object>> dataListMap, String levelPath){

        dataMap.put(levelPath, dataListMap);

        String result = "";
        if(dataListMap != null){
            for (int i = 0; i < dataListMap.size(); i++) {
                result += dataListMap.get(i).get("name");
            }
        }

        if(dataListMap == null){
            return;
        }

        for (int i = 0; i < dataListMap.size(); i++) {
            Map<String, Object> map = dataListMap.get(i);
            List<Map<String, Object>> nextListMap = (List<Map<String, Object>>) map.get("nextData");
            initNextLevelData(nextListMap, levelPath + "," + i);
        }

    }

    private void initView(Context context) {
        setContentView(R.layout.dialog_wheel);
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);

        ll_wheel = (LinearLayout) findViewById(R.id.ll_wheel);
        ll_wheel.removeAllViews();
        initNextLevelData(dataListMap);

        int maxLength = 0;
        for (Iterator iterator = dataMap.keySet().iterator();iterator.hasNext();){
            String key = (String) iterator.next();
            int length = key.split(",", -1).length;
            if(length > maxLength){
                maxLength = length;
            }
        }

        maxLength--;

        String key = "-1";
        for (int i = 0; i < maxLength; i++) {
            WheelView wheelView = new WheelView(getContext());
            ll_wheel.addView(wheelView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
            List<Map<String, Object>> data = dataMap.get(key);
            String[] items = new String[data == null ? 0 : data.size()];
            for (int j = 0; j < items.length; j++) {
                items[j] = (String) data.get(j).get("name");
            }
            ArrayWheelAdapter adapter = new ArrayWheelAdapter(getContext(), items);
            wheelView.setViewAdapter(adapter);
            wheelView.addChangingListener(this);
            key += "," + 0;
        }

        if(chooseDatas != null){
            String chooseKey = "-1";
            for(int i = 0; i < chooseDatas.length; i++){
                if(i < ll_wheel.getChildCount() && !Strings.isNullOrEmpty(chooseDatas[i])){
                    List<Map<String, Object>> data = dataMap.get(chooseKey);
                    WheelView wheel = (WheelView) ll_wheel.getChildAt(i);
                    int nextLevel = 0;
                    if(data != null){
                        for(int j = 0;j < data.size(); j++){
                            Map<String, Object> map = data.get(j);
                            if(chooseDatas[i].equals(map.get("name"))){
                                wheel.setCurrentItem(j);
                                nextLevel = j;
                                break;
                            }
                        }
                    }
                    chooseKey += "," + nextLevel;
                }
            }
        }

        findViewById(R.id.btn_confirm).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                if(clickSureListener != null){
                    selectedDataList.clear();
                    String key = "-1";
                    for (int i = 0; i < ll_wheel.getChildCount(); i++) {
                        WheelView eachWheelView = (WheelView) ll_wheel.getChildAt(i);
                        if(dataMap.get(key).size() > eachWheelView.getCurrentItem()){
                            selectedDataList.add(dataMap.get(key).get(eachWheelView.getCurrentItem()));
                        }else{
                            selectedDataList.add(null);
                        }
                        key += "," + eachWheelView.getCurrentItem();
                    }
                    clickSureListener.onSelected(selectedDataList);
                }
                break;
            case R.id.btn_cancel:
                break;
        }
        dismiss();
    }


    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {

        WheelView nextWheel = null;
         List<Map<String,Object>> nextListMap = null;
        String key = "-1";
        for (int i = 0; i < ll_wheel.getChildCount(); i++) {
            WheelView eachWheelView = (WheelView) ll_wheel.getChildAt(i);
            key += "," + eachWheelView.getCurrentItem();
            if(eachWheelView == wheel && i !=  ll_wheel.getChildCount() - 1){
                for (int j = i + 1; j < ll_wheel.getChildCount(); j++) {
                    nextWheel = (WheelView) ll_wheel.getChildAt(j);
                    nextListMap = dataMap.get(key);
                    String[] items = new String[nextListMap == null ? 0 : nextListMap.size()];
                    if(items.length == 0){
                        items = new String[]{" "}; //空字符d串占高度，否则控件BUG
                    }else{
                        for (int k = 0; k < items.length; k++) {
                            items[k] = (String) nextListMap.get(k).get("name");
                        }
                    }
                    ArrayWheelAdapter adapter = new ArrayWheelAdapter(getContext(), items);
                    nextWheel.setViewAdapter(adapter);
                    nextWheel.setCurrentItem(0);
                    key += ",0";
                }
                break;
            }
        }
    }

    private ClickSureListener clickSureListener;

    public void setClickSureListener(ClickSureListener clickSureListener) {
        this.clickSureListener = clickSureListener;
    }

    public interface ClickSureListener {
        /**
         * 确定点击
         * @param dataList 当前选中每级的数据集合
         */
        void onSelected(List<Map<String, Object>> dataList);
    }


}
